﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    timer = new QTimer(this);

    ConnectSlots();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::OpenCamera()
{
    //ui->CameraView->width()
    cap.set(cv::CAP_PROP_FRAME_WIDTH, 800);
    //ui->CameraView->height()
    cap.set(cv::CAP_PROP_FRAME_HEIGHT, 600);
    cap.open(0);
//    qDebug() << cap.get(cv::CAP_PROP_FRAME_WIDTH) << ' '
//             << cap.get(cv::CAP_PROP_FRAME_HEIGHT) << "a\n";
    timer->start(50); // 单位ms,可以自己调整
}

void MainWindow::closeCamera()
{
    timer->stop();
    cap.release();
    ui->CameraView->clear();
}

void MainWindow::GetFrame()
{

}

QImage MainWindow::mat2qimage(const cv::Mat& mat)
{
    cvtColor(mat, mat, cv::COLOR_BGR2RGB);
    return QImage((const unsigned char*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
}

void MainWindow::ConnectSlots()
{
    //QTimer
    connect(timer,SIGNAL(timeout()),this,SLOT(GetFrame()));

    //OpenCamera
    connect(ui->pushButton_open, SIGNAL(clicked()), this, SLOT(OpenCamera()));
    //close
    connect(ui->pushButton_close, SIGNAL(clicked()), this, SLOT(closeCamera()));

}
